package com.lt.test.provider.service;


import com.alibaba.dubbo.config.annotation.Service;
import com.lt.test.api.KtParamTypeService;
import com.lt.test.common.exception.CustomException;
import com.lt.test.common.util.StringUtils;
import com.lt.test.provider.dao.KtParamTypeDao;
import com.lt.test.entity.DTO.PageDTO;
import com.lt.test.provider.mapper.KtCountMapper;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service(version = "1.0.0")
public class KtParamTypeServiceImpl implements KtParamTypeService{
	
	@Autowired
	 private KtParamTypeDao KtParamTypeDao;
	
    @Autowired
    KtCountMapper ktCountMapper;
	
	/**
	 * 获取参数类型列表
	 */
	@Override
	public PageDTO<Map<String, Object>> getParamTypeList(Integer page, Integer pageSize, String filters) {
		
		Long beginNum = (long) ((page - 1) * pageSize + 1);
		Long endNum = beginNum + pageSize - 1;

		List<Map<String,Object>> typeList = null;
		int recordCount = 0;
		String paramType = null;
		if(filters != null && !filters.trim().equals("")) {
			JSONObject jsonObject = JSONObject.fromObject(filters);
			JSONArray jsonArray = (JSONArray) jsonObject.get("rules");
			for(int i = 0; i < jsonArray.size(); i++) {
				JSONObject result = (JSONObject) jsonArray.get(i);
				if("paramType".equals(result.get("field")) && !StringUtils.isNullOrEmpty(result.getString("data"))){
					paramType =  result.getString("data");
				}
			}
		}
		String state = "1";
		typeList = KtParamTypeDao.getParamTypeList(state,paramType,beginNum,endNum);
		recordCount = Integer.valueOf(KtParamTypeDao.getParamTypeListSize(state,paramType).get("total").toString());
		PageDTO<Map<String,Object>> result = new PageDTO<>(page, pageSize, typeList, recordCount);
		return result;
	}
	
	
	/**
	 * 新增（更改）参数类型列表
	 */
	@Override
	public void saveParamType(Map parameter) throws CustomException{
		String id = (String) parameter.get("dictId");
		if(StringUtils.isNullOrEmpty(id)){
			parameter.put("createTime", new Date());
			parameter.put("state", 1);
			KtParamTypeDao.addParams(parameter);
		}else{
			parameter.put("modifyTime", new Date());
			KtParamTypeDao.updateParams(parameter);
		}
	}
	 
	/**
	 * 删除某条参数类型
	 */
	@Override
	public void deleteParamType(Map parameter) throws CustomException{
		parameter.put("state", "0");
		KtParamTypeDao.deleteParams(parameter);
	}


	/**
	 * 获取参数类型下拉框
	 */
	@Override
	public PageDTO<Map<String, Object>> getParamType() {
		
		String state = "1";
		List<Map<String,Object>> typeList = KtParamTypeDao.getParamType(state);
		
		Integer page = 1;
		Integer pageSize = 1;
		int recordCount = 1;
		PageDTO<Map<String,Object>> result = new PageDTO<>(page, pageSize, typeList, recordCount);
		return result;
	}
}
